<template>
  <div class="training-panel">
    <ControlPanel />
    
    <div class="view-mode-selector">
      <label>选择显示模式：</label>
      <label v-for="mode in viewModes" :key="mode.value">
        <input 
          type="radio" 
          v-model="currentViewMode" 
          :value="mode.value" 
          @change="changeViewMode"
        />
        {{ mode.label }}
      </label>
    </div>
    
    <h2>逻辑训练图（Replica 状态）</h2>
    
    <div class="replica-groups">
      <div v-for="(replicas, groupId) in displayedGroups" :key="groupId" class="replica-group">
        <h3>{{ titlePrefix }} = {{ groupId }}</h3>
        <div class="replicas-container">
          <ReplicaStatus 
            v-for="(status, index) in replicas" 
            :key="index"
            :status="status"
            :details="getDetails(groupId, index)"
            :index="index"
            :groupId="parseInt(groupId)"
          />
        </div>
      </div>
    </div>
    <CommGraph />123
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useTrainingStore } from '../stores/training'
import ControlPanel from './ControlPanel.vue'
import ReplicaStatus from './ReplicaStatus.vue'
import CommGraph from './CommGraph.vue'

const trainingStore = useTrainingStore()

const viewModes = [
  { value: 'overall', label: 'overall' },
  { value: 'on_training', label: 'on_training' }
]

const currentViewMode = ref('overall')

const displayedGroups = computed(() => {
  return currentViewMode.value === 'overall' 
    ? trainingStore.replicaGroups 
    : trainingStore.replicaId
})

const titlePrefix = computed(() => {
  return currentViewMode.value === 'overall' 
    ? 'Replica Group ID' 
    : 'Replica ID'
})

function getDetails(groupId, index) {
  const key = `${groupId}-${index}`
  return trainingStore.replicaDetails[key] || {
    gpu: "Unknown", batch: "-", lr: "-", opt: "-"
  }
}

function changeViewMode() {
  trainingStore.setViewMode(currentViewMode.value)
}
</script>

<style scoped>
/* Add this to the existing styles */
.training-panel {
  min-height: 100%;
  padding: 20px;
  padding-bottom: 40px; /* Give some space at bottom */
  background-color: #fff;
}


.view-mode-selector {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 10px;
}

.view-mode-selector label {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}

h2, h3 {
  margin: 0;
}

h2 {
  font-size: 1.2em;
  margin-bottom: 15px;
}

h3 {
  font-size: 1.1em;
  margin-bottom: 10px;
}

.replica-groups {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.replica-group {
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 15px;
}

.replicas-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 15px;
  padding: 10px;
}
</style>